// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-06 10:43:48
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 14:41:54
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Element/CHBDYPParser.cpp
 */

#include "Import/BDFImport/BulkData/Element/CHBDYPParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CHBDYPData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CHBDYP);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CHBDYP/CHBDYP.md>
*/
// 以下需要修改
enum CHBDYPKey : std::uint8_t
{
    // Row 1
    CHBDYP_EID     = 2,   // 表面元素识别编号
    CHBDYP_PID     = 3,   // 属性条目识别编号
    CHBDYP_TYPE    = 4,   // 表面类型
    CHBDYP_IVIEWF  = 5,   // 前面的VIEW条目识别编号
    CHBDYP_IVIEWB  = 6,   // 后面的VIEW条目识别编号
    CHBDYP_G1      = 7,   // 限制表面的网格点识别编号
    CHBDYP_G2      = 8,   // E
    CHBDYP_G0      = 9,   // GO 方向网格点
    CHBDYP_RADMIDF = 12,  // 前面的RADM条目识别编号
    CHBDYP_RADMIDB = 13,  // 后面的RADM条目识别编号

    CHBDYP_GMID = 14,     // 如果与线型表面元素一起使用，侧面节点的网格点识别编号
    CHBDYP_CE   = 15,     // 用于定义方向向量的坐标系
    CHBDYP_E1   = 16,     // 在坐标系CE中方向向量的分量。方向向量的原点是网格点G1
    CHBDYP_E2   = 17,     // 在坐标系CE中方向向量的分量。方向向量的原点是网格点G1
    CHBDYP_E3   = 18,     // 在坐标系CE中方向向量的分量。方向向量的原点是网格点G1

};

namespace
{
    Common::I64 getType (const std::string& type)
    {
        static std::map<std::string, Common::I64> typeMap = {
            { "POINT", 1 }, { "LINE", 2 }, { "ELCYL", 6 }, { "FTUBE", 7 }, { "TUBE", 10 }
        };
        auto iter = typeMap.find (type);
        if (iter == typeMap.end ())
        {
            return 1;
        }
        return typeMap[type];
    }
}  // namespace

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCHBDYP (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::CHBDYPData tempCHBDYP;

    // TODO(bug 001) m_dominId的正确填写
    tempCHBDYP.m_domainId = 1;

    int curIDKey = 1;  // 当前字符串是第几项

    for (auto curStrKey : dividedEntryInfo)
    {
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case CHBDYP_EID:
            tempCHBDYP.m_id = convertToI64 (curStrKey);
            break;
        case CHBDYP_PID:
            tempCHBDYP.m_pId = convertToI64 (curStrKey);
            break;
        case CHBDYP_TYPE:
            boost::trim (curStrKey);
            tempCHBDYP.m_TYPE = getType (curStrKey);
            break;
        case CHBDYP_IVIEWF:
            tempCHBDYP.m_IVIEWF = convertToI64 (curStrKey);
            break;
        case CHBDYP_IVIEWB:
            tempCHBDYP.m_IVIEWB = convertToI64 (curStrKey);
            break;
        case CHBDYP_G1:
            tempCHBDYP.m_g[0] = convertToI64 (curStrKey);
            break;
        case CHBDYP_G2:
            tempCHBDYP.m_g[1] = convertToI64 (curStrKey);
            break;
        case CHBDYP_G0:
            tempCHBDYP.m_GO = convertToI64 (curStrKey);
            break;
        case CHBDYP_RADMIDF:
            tempCHBDYP.m_RADMIDF = convertToI64 (curStrKey);
            break;
        case CHBDYP_RADMIDB:
            tempCHBDYP.m_RADMIDB = convertToI64 (curStrKey);
            break;
        case CHBDYP_GMID:
            tempCHBDYP.m_DISLIN = convertToI64 (curStrKey);
            break;
        case CHBDYP_CE:
            tempCHBDYP.m_CE = convertToI64 (curStrKey);
            break;
        case CHBDYP_E1:
            tempCHBDYP.m_E1 = convertToReal (curStrKey);
            break;
        case CHBDYP_E2:
            tempCHBDYP.m_E2 = convertToReal (curStrKey);
            break;
        case CHBDYP_E3:
            tempCHBDYP.m_E3 = convertToReal (curStrKey);
            break;
        default:
            break;
        }

        curIDKey++;
    }

    return dbServiceFactory->get<IElementService> ()->Add (tempCHBDYP);
}
